package com.sss.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sss.entity.SysUser;
import com.sss.service.SysUserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.sss.util.R;

import java.util.List;
import java.util.Map;

/**
 * 用户信息表(SysUser)表控制层
 *
 * @author makejava
 * @since 2023-12-14 16:55:32
 */
@RestController
@RequestMapping("/api/sysUser")
@CrossOrigin
public class SysUserController {
   private final Logger logger = LogManager.getLogger(SysUserController.class);

    /**
     * 服务对象
     */
    @Autowired
    private SysUserService sysUserService;

    /**
     * 分页查询
     *前端的发送请求的方式是表单形式 :'application/x-www-form-urlencoded'
     * 后端需要用@RequestParam接收
     * 前端发json格式，map需要用@RequestBody接收
     * @return 查询结果
     */
    @PostMapping("/list")
    public R queryByPage(@RequestParam Map<String, Object> map){
//        logger.debug("查询全部接收的map:{}",map);

        int pageNum = Integer.parseInt ((String) map.get("page"));
        int pageSize = Integer.parseInt ((String) map.get("limit"));

        PageHelper.startPage(pageNum,pageSize);

        List<SysUser> list = this.sysUserService.queryByPage(map);

        PageInfo<SysUser> info = new PageInfo<>(list);

        return R.ok(info);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public R queryById(@PathVariable("id") Long id) {
        return R.ok(this.sysUserService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param sysUser 实体
     * @return 新增结果
     */
    @PostMapping("/save")
    public R add(SysUser sysUser) {
        this.sysUserService.insert(sysUser);
        return R.ok();
    }

    /**
     * 编辑数据
     *只有json需要用@Requestbody
     * 其他正常接
     * @param sysUser 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public R edit(SysUser sysUser) {
        logger.debug("接收的sysUser："+sysUser);
        return R.ok(this.sysUserService.update(sysUser));
    }


    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/del/{id}")
    public R deleteById(@PathVariable Long id) {
        logger.debug("接收的id"+id);
        boolean b = this.sysUserService.deleteById(id);
        return b ? R.ok() : R.fail();
    }

    /**
     * 批量删除数据
     *
     * @return 删除是否成功
     */
    @DeleteMapping("/del/batch")
    public R deleteBatch(@RequestBody List<Integer> list) {
        System.out.println("批量删除接收的id---"+list);
        int num = this.sysUserService.deleteBatch(list);
        return num > 0 ? R.ok() : R.fail();
    }


    /**
     * 登录
     * @return 查询结果
     */
    @PostMapping("/login")
    public R loginSys(String username,String password){
        System.out.println(username+password);

        SysUser sysUser = this.sysUserService.loginSys(username,password);

//        logger.debug("登录查询返回的对象：{}",sysUser);

        if (sysUser != null){
            return R.ok(sysUser);
        }else {
            return R.fail();
        }
    }
}



